package com.caiyi.tips.user.controller;

import com.caiyi.tips.core.Constant.PublicConstant;
import com.caiyi.tips.core.VO.ResultVO;
import com.caiyi.tips.core.enums.ResultEnum;
import com.caiyi.tips.core.util.CheckUtil;
import com.caiyi.tips.core.util.StringUtil;
import com.caiyi.tips.core.util.TipsUtil;
import com.caiyi.tips.user.DTO.ExpertRegisterDTO;
import com.caiyi.tips.user.bean.ExpertBean;
import com.caiyi.tips.user.exception.UserException;
import com.caiyi.tips.user.service.ExpertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/* 专家相关controller
 * @author Cliff.ZhouYang
 * @date 2017/11/20 15:42
 */

@RequestMapping("/expert")
@RestController
@Slf4j
@Validated
@Api(value = "专家接口", tags = "4.推单专家", description = "专家注册、登录等相关接口", position = 2, protocols = "http")
public class ExpertController {
    @Autowired
    private ExpertService expertService;
    @Autowired
    StringRedisTemplate redisTemplate;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ApiOperation(value = "注册", notes = "需要前置接口，发送短信验证码\n\n" +
            "密码字段加密原则: 加盐md5的32位小写\n\n" +
            "   {\n\n" +
            "       \"code\": \"0\",\n\n" +
            "       \"msg\": \"注册成功\",\n\n" +
            "       \"data\": {\n\n" +
            "           \"pass\": 0,//0用户注册状态(0已注册 1身份已认证 2有料号已设置 3审核通过)\n\n" +
            "           \"success\": 1//0不通过1处理中2通过\n\n" +
            "       }\n\n" +
            "   }")
    public ResultVO register(HttpServletRequest request, ExpertRegisterDTO dto) {
        if (StringUtils.isEmpty(dto.getPhone())) {
            throw new UserException("手机号不能为空");
        }
        if (!CheckUtil.isMobilephone(dto.getPhone())) {
            throw new UserException("手机号格式不正确");
        }
        if (StringUtils.isEmpty(dto.getPassword())) {
            throw new UserException("密码不能为空");
        }
        if (StringUtils.isEmpty(dto.getRandom())) {
            throw new UserException("验证码不能为空");
        }
        ExpertBean expertBean = new ExpertBean();
        BeanUtils.copyProperties(dto, expertBean);
        expertBean.setIpAddr(TipsUtil.getRealIp(request));
        return expertService.register(expertBean, request);
    }

    @PostMapping(value = "/login")
    @ApiOperation(value = "登录接口", notes = "登录")

    public ResultVO login(
            @ApiParam(required = true, name = "username", value = "用户名", defaultValue = "18017254180")
            @RequestParam("username") String username,
            @ApiParam(required = true, name = "password", value = "密码", defaultValue = "123456")
            @RequestParam("password") String password,
            @ApiParam(required = false, name = "imgYzm", value = "图形验证码")
            @RequestParam(value = "imgYzm", required = false) String imgYzm) {
        
        if (StringUtil.isEmpty(username)) {
            throw new UserException("用户名不能为空");
        }
        if (StringUtil.isEmpty(password)) {
            throw new UserException("密码不能为空");
        }
        ExpertBean bean = new ExpertBean();
        bean.setUserid(username);
        bean.setPassword(password);
        return expertService.login(bean);
    }

    @PostMapping(value = "/logout")
    public ResultVO logout() {
        return null;
    }
}
